##
#
# Snaarpy: Guitar chord parser and formatter
# Copyright (C) 2009 Alberto Gonzalez
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
##

'''
Base library for tab file input
'''

import sys
import logging
logger = logging.getLogger(__name__)

from snaarpy import SnaarpyPlugin

class InputFileReader:
    """
    Base abstract class for all input file formats.
    
    Must provide the following attributes:
    
      extension
    
    Must provide the following methods:
    
      read()
    """
    
    def __init__(self, path):
        self.file_path = path
    
    __metaclass__ = SnaarpyPlugin

# import all plugins
import snaarpy.input.txt
import snaarpy.input.chordpro
for ext, plugin in InputFileReader.plugins.iteritems():
    logger.debug("Loaded input plugin for %s: %s" % (ext, plugin))

class UnsupportedInputError(Exception):
    pass

def print_plugin_list():
    print "Available input filters:"
    for plugin in InputFileReader.plugins.itervalues():
        print "    %s\t%s" % (plugin.extension, plugin.description)

def read_file(path, extension):
    try:
        plugin = InputFileReader.plugins[extension]
    except KeyError:
        raise UnsupportedInputError("Unsupported file format '%s' for '%s'" % (extension, path))
    reader = plugin(path)
    return reader.read()
        
